package com.lwx.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwx.sys.constant.Constant;
import com.lwx.sys.domain.Role;
import com.lwx.sys.mapper.RoleMapper;
import com.lwx.sys.service.RoleService;
import com.lwx.sys.utils.DataGridView;
import com.lwx.sys.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lwx
 * @since 2020-04-14
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public DataGridView loadAllRole(RoleVo roleVo) {
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(roleVo.getName()),"name",roleVo.getName());
        qw.like(StringUtils.isNotBlank(roleVo.getRemark()),"remark",roleVo.getRemark());
        qw.orderByDesc("createtime");
        //分页
        Page page = new Page<>(roleVo.getPage(), roleVo.getLimit());
        //查询数据库
        roleMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    //增删改都要加事务
    @Transactional
    @Override
    public void deleteRole(Integer id) {
        //因为有外键关联  先删除中间表
        //sys_role_permission
        roleMapper.deleteRolePermissionByPid(id);
        //sys_role_user
        roleMapper.deleteRoleUserByRoleId(id);
        roleMapper.deleteById(id);
    }

    @Transactional
    @Override
    public void batchDeleteRole(Integer[] ids) {
        Arrays.stream(ids).forEach(id ->{
            //sys_role_permission
            roleMapper.deleteRolePermissionByPid(id);
            //sys_role_user
            roleMapper.deleteRoleUserByRoleId(id);
        });
        roleMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Transactional
    @Override
    public void addRole(Role role) {
        roleMapper.insert(role);
    }

    @Transactional
    @Override
    public void updateRole(Role role) {
        roleMapper.updateById(role);
    }

    @Override
    public void saveRolePermission(Integer roleId, Integer[] pids) {
        //先删中间表
        roleMapper.deleteRolePermissionByPid(roleId);
        //再插入中间表
        for (Integer pid:pids){
            roleMapper.saveRolePermission(roleId,pid);
        }
    }

    @Override
    public DataGridView queryRolesByUserId(Integer id) {

        QueryWrapper<Role> qw  = new QueryWrapper<>();
        qw.eq("available", Constant.AVAILABLE_TRUE);
        //所有可用角色
        List<Role> allRoles = this.roleMapper.selectList(qw);

        //查询当前用户拥有的角色id
        List<Integer> roleIds = roleMapper.selectRoleIdsByUserId(id);

        //当前用户拥有角色
        List<Role> currentUserRoles = new ArrayList<>();
        if (roleIds != null && roleIds.size()!=0){
            qw.in("id",roleIds);
            currentUserRoles = roleMapper.selectList(qw);
        }

        //判断用户角色是不是所有角色里，给其标记
        List<Map<String,Object>> res = new ArrayList<>();
        for(Role role1:allRoles){
            boolean LAY_CHECKD =false;
            for (Role role2:currentUserRoles){
                if (role1.getId().equals(role2.getId())){
                    LAY_CHECKD = true;
                    break;
                }
            }
            Map<String , Object>  map= new HashMap<>();
            map.put("id",role1.getId());
            map.put("name",role1.getName());
            map.put("remark",role1.getRemark());
            map.put("createtime",role1.getCreatetime());
            map.put("LAY_CHECKED",LAY_CHECKD);

            res.add(map);
        }

        return new DataGridView(res);
    }

    @Override
    public List<String> queryRoleNamesByUserId(Integer id) {
        //获取登录用户的所有的角色id
        List<Integer> roleIds = roleMapper.selectRoleIdsByUserId(id);
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq("available",Constant.AVAILABLE_TRUE);
        List<Role> currentRoles = new ArrayList<>();
        if (roleIds != null && roleIds.size()!=0){
            qw.in("id",roleIds);
            currentRoles = roleMapper.selectList(qw);
        }
        List<String> roles = new ArrayList<>();
        for (Role role:currentRoles){
            roles.add(role.getName());

        }
        return roles;
    }
}
